/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.component.redis;

import com.workcell.workdata.im.entity.MicroServer;
import com.workcell.workdata.im.entity.proto.MicroServerProto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MicroServerRedisTemplate extends RedisTemplate<String, MicroServer>
		implements RedisSerializer<MicroServer> {

	private static final String CACHE_PREFIX = "MICRO_SERVER_";

	@Autowired
	public MicroServerRedisTemplate(RedisConnectionFactory connectionFactory) {
		StringRedisSerializer stringSerializer = new StringRedisSerializer();

		setKeySerializer(stringSerializer);
		setHashKeySerializer(stringSerializer);

		setValueSerializer(this);
		setHashValueSerializer(this);
		setConnectionFactory(connectionFactory);
		afterPropertiesSet();
	}

	public void save(MicroServer server) {
		String key = CACHE_PREFIX + server.getId();
		super.boundValueOps(key).set(server);
	}

	public MicroServer get(long id) {
		String key = CACHE_PREFIX + id;
		return super.boundValueOps(key).get();
	}

	public void remove(long id) {
		String key = CACHE_PREFIX + id;
		super.delete(key);
	}

	@Override
	public byte[] serialize(MicroServer server) throws SerializationException {
		MicroServerProto.Model.Builder builder = MicroServerProto.Model.newBuilder();

		builder.setId(server.getId());
		builder.setAccount(server.getAccount());
		builder.setName(server.getName());
		if (server.getDescription() != null) {
			builder.setDescription(server.getDescription());
		}
		if (server.getWebhook() != null) {
			builder.setWebhook(server.getWebhook());
		}
		if (server.getWebsite() != null) {
			builder.setWebsite(server.getWebsite());
		}
		if (server.getGreet() != null) {
			builder.setGreet(server.getGreet());
		}

		builder.setTimestamp(server.getCreateTime().getTime());
		return builder.build().toByteArray();
	}

	@Override
	public MicroServer deserialize(byte[] bytes) throws SerializationException {

		try {
			MicroServerProto.Model proto = MicroServerProto.Model.parseFrom(bytes);
			MicroServer server = new MicroServer();
			server.setId(proto.getId());
			server.setAccount(StringUtils.trimToNull(proto.getAccount()));
			server.setName(StringUtils.trimToNull(proto.getName()));
			server.setWebhook(StringUtils.trimToNull(proto.getWebhook()));
			server.setDescription(StringUtils.trimToNull(proto.getDescription()));
			server.setGreet(StringUtils.trimToNull(proto.getGreet()));
			server.setWebsite(StringUtils.trimToNull(proto.getWebsite()));
			server.setCreateTime(new Date(proto.getTimestamp()));
			return server;

		} catch (Exception ignore) {
			return null;
		}
	}

}
